package com.wechat.page;

import java.util.List;

import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.Screen;
import com.swak.fx.support.Window;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIStackPane;
import com.swak.ui.EventListener;
import com.swak.utils.StringUtils;
import com.wechat.page.chat.ChatPage;
import com.wechat.page.contact.ContactPage;
import com.wechat.page.navbar.NavbarPage;

import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * 首页
 * 
 * @author lifeng
 */
@FXMLView(title = "首页", value = "MainPage.fxml", css = "MainPage.css", stageStyle = "TRANSPARENT")
public class MainPage extends Window implements EventListener {

	public static final int ADJUST_SIZE = 10;

	@FXML
	private StackPane navbarPane;
	@FXML
	private UIStackPane mainPane;
	private NavbarPage navbarPage;

	public MainPage(Stage stage) {
		super(stage, ADJUST_SIZE);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.storePageConfigAble = true;

		// 大小
//		double w = Double.parseDouble(this.getPageConfig(UI.Conts.Settings_Page_Width, "740"));
//		double h = Double.parseDouble(this.getPageConfig(UI.Conts.Settings_Page_Height, "520"));
		this.root.setPrefWidth(740);
		this.root.setPrefHeight(520);
		this.root.setMinSize(740, 520);

		// 位置
//		double x =  Double.parseDouble(this.getPageConfig(UI.Conts.Settings_Page_X, "0"));
//		double y = Double.parseDouble(this.getPageConfig(UI.Conts.Settings_Page_Y, "0"));
//		this.root.setLayoutX(x);
//		this.root.setLayoutY(y);

		// 注册页面
		this.navbarPage = this.NewPage(NavbarPage.class);

		// 添加页面
		this.navbarPane.getChildren().add(navbarPage.getRoot());

		GridPane.setMargin(navbarPane, new Insets(ADJUST_SIZE, 0, ADJUST_SIZE, ADJUST_SIZE));
		GridPane.setMargin(mainPane, new Insets(ADJUST_SIZE, ADJUST_SIZE, ADJUST_SIZE, 0));
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.moveWindowWhit(navbarPane);
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.navbarPage.chatActiveProperty().addListener((ob, o, n) -> this.toggleActivePage(ChatPage.class, n));
		this.navbarPage.contactActiveProperty().addListener((ob, o, n) -> this.toggleActivePage(ContactPage.class, n));
	}

	private <T extends AbstractPage> void toggleActivePage(Class<T> pageClass, boolean active) {
		if (active) {
			AbstractPage page = this.NewPage(pageClass);
			this.mainPane.switchTop(page.getRoot());
			page.fireActivated();
		}
	}

	@Override
	public void initializeFinish() {
		super.initializeFinish();
		this.navbarPage.chatActiveProperty().set(true);
		this.layoutMainPage();
	}

	/**
	 * 目标：在之前的屏幕中居中显示
	 */
	protected void layoutMainPage() {

		// 之前的屏幕 (有可能不存在，所有不需要处理)
		String screenStr = this.getPageConfig(UI.Conts.Settings_Page_Screen, null);
		if (StringUtils.isBlank(screenStr)) {
			return;
		}

		// 查找屏幕
		Screen screen = this.getScreen();
		List<Screen> screens = this.getScreens();
		for (Screen sc : screens) {
			if (String.valueOf(sc.getKey()).equals(screenStr)) {
				screen = sc;
				break;
			}
		}

		// 之前的宽度 -- 处理一个最大值
		double w = Double.parseDouble(this.getPageConfig(UI.Conts.Settings_Page_Width, "740"));
		double h = Double.parseDouble(this.getPageConfig(UI.Conts.Settings_Page_Height, "520"));
		this.layoutPageInScreen(screen, w, h);
	}

	@Override
	public void mounted() {
		super.mounted();
	}

	@Override
	public void destory() {
		super.destory();
	}
}